package com.bestom.bluetoothlock.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bestom.bluetoothlock.LockApplication;
import com.bestom.bluetoothlock.R;
import com.bestom.bluetoothlock.adapter.RecordListAdapter;
import com.bestom.bluetoothlock.bluetooth.BleServiceManage;
import com.bestom.bluetoothlock.bluetooth.BraceletInstructions;
import com.bestom.bluetoothlock.entity.DeviceInfo;
import com.bestom.bluetoothlock.entity.Record;
import com.bestom.bluetoothlock.util.Constants;
import com.bestom.bluetoothlock.util.CurrencyDataManage;
import com.bestom.bluetoothlock.util.ToastUtil;
import com.bestom.bluetoothlock.views.SearchDialog;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UseRecordActivity extends BaseActivity {
    /* access modifiers changed from: private */
    public RecordListAdapter adapter;
    /* access modifiers changed from: private */
    public BleServiceManage bleManage;
    /* access modifiers changed from: private */
    public DeviceInfo device;
    /* access modifiers changed from: private */
    public SearchDialog dialog;

    private int index = 0;
    @BindView(R.id.iv_left)
    ImageView iv_left;
    /* access modifiers changed from: private */
    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 1 && UseRecordActivity.this.recordList.size() > 0 && UseRecordActivity.this.device != null) {
                TextView textView = UseRecordActivity.this.tv_title;
                textView.setText(UseRecordActivity.this.getString(R.string.record) + "(" + UseRecordActivity.this.recordList.size() + ")");

                LockApplication.getSharedPreferencesHelper().saveInt("record_" + UseRecordActivity.this.device.getLockmac() + "_count", UseRecordActivity.this.recordList.size());
                for (int i = 0; i < UseRecordActivity.this.recordList.size(); i++) {

                    LockApplication.getSharedPreferencesHelper().saveLong("record_" + UseRecordActivity.this.device.getLockmac() + "_time" + i, ((Record) UseRecordActivity.this.recordList.get(i)).getTime());

                    LockApplication.getSharedPreferencesHelper().saveString("record_" + UseRecordActivity.this.device.getLockmac() + "_content" + i, ((Record) UseRecordActivity.this.recordList.get(i)).getContent());

                    LockApplication.getSharedPreferencesHelper().saveString("record_" + UseRecordActivity.this.device.getLockmac() + "_name" + i, ((Record) UseRecordActivity.this.recordList.get(i)).getNickname());
                }
            }
        }
    };
    /* access modifiers changed from: private */
    public List<Record> recordList = new ArrayList();
    @BindView(R.id.record_list)
    ListView record_list;
    /* access modifiers changed from: private */
    public SortClass sortClass;
    @BindView(R.id.tv_right)
    TextView tv_right;
    @BindView(R.id.tv_title)
    TextView tv_title;

    public long byteArrayToLong(byte b, byte b2, byte b3, byte b4) {
        return ((long) ((b2 & 255) << 8)) | ((long) (b & 255)) | ((long) ((b3 & 255) << 16)) | ((long) ((b4 & 255) << 24));
    }

    public class SortClass implements Comparator {
        public SortClass() {
        }

        public int compare(Object obj, Object obj2) {
            Record record = (Record) obj;
            Record record2 = (Record) obj2;
            if (record.getTime() > record2.getTime()) {
                return -1;
            }
            return record.getTime() < record2.getTime() ? 1 : 0;
        }
    }

    public byte[] hexStringToBytes(String str) {
        if (str == null || str.equals("")) {
            return null;
        }
        String upperCase = str.toUpperCase();
        int length = upperCase.length() / 2;
        char[] charArray = upperCase.toCharArray();
        byte[] bArr = new byte[length];
        for (int i = 0; i < length; i++) {
            int i2 = i * 2;
            bArr[i] = (byte) (charToByte(charArray[i2 + 1]) | (charToByte(charArray[i2]) << 4));
        }
        return bArr;
    }

    private byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /* access modifiers changed from: protected */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_use_record);
        ButterKnife.bind((Activity) this);
        this.sortClass = new SortClass();
        this.device = (DeviceInfo) getIntent().getSerializableExtra(Constants.KEY_VALUE);
        initView();
    }

    private void initView() {
        this.tv_title.setText(getString(R.string.record));
        this.iv_left.setVisibility(View.VISIBLE);
        this.tv_right.setText(getString(R.string.fingerprint_clear));
        this.tv_right.setVisibility(View.VISIBLE);
        this.adapter = new RecordListAdapter(this);
        this.adapter.setData(this.recordList);
        this.record_list.setAdapter(this.adapter);
        this.bleManage = BleServiceManage.getInstance(this);
        if (this.device != null) {
            int i = LockApplication.getSharedPreferencesHelper().getInt("record_" + this.device.getLockmac() + "_count");
            if (i > 0) {
                TextView textView = this.tv_title;
                textView.setText(getString(R.string.record) + "(" + i + ")");
                for (int i2 = 0; i2 < i; i2++) {
                    Record record = new Record();
                    record.setTime(LockApplication.getSharedPreferencesHelper().getLong("record_" + this.device.getLockmac() + "_time" + i2));
                    record.setContent(LockApplication.getSharedPreferencesHelper().getString("record_" + this.device.getLockmac() + "_content" + i2));
                    record.setNickname(LockApplication.getSharedPreferencesHelper().getString("record_" + this.device.getLockmac() + "_name" + i2));
                    this.recordList.add(record);
                }
                this.adapter.notifyDataSetChanged();
            }
        }
        if (this.bleManage.isConnect()) {
            this.bleManage.sendData(BraceletInstructions.getFingerRecordInstructions());
        }
    }

    public void onClearAllClicked(View view) {
        this.recordList.clear();
        TextView textView = this.tv_title;
        textView.setText(getString(R.string.record) + "(" + this.recordList.size() + ")");
        LockApplication.getSharedPreferencesHelper().saveInt("record_" + this.device.getLockmac() + "_count", this.recordList.size());
        this.adapter.notifyDataSetChanged();
    }

    private void showSearchDialog() {
        if (!this.bleManage.isConnect()) {
            ToastUtil.toast(getString(R.string.not_connected_bluetooth));
            return;
        }
        if (this.dialog == null) {
            this.dialog = new SearchDialog(this);
        }
        this.dialog.show();
        this.dialog.setTitle(getString(R.string.are_the_search));
        Window window = this.dialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = -1;
        window.setAttributes(attributes);
        this.bleManage.sendData(BraceletInstructions.getFingerRecordInstructions());
        new Handler().postDelayed(new Runnable() {
            public void run() {
                UseRecordActivity.this.dialog.dismiss();
            }
        }, 5000);
    }

    /* access modifiers changed from: package-private */
    @OnClick({R.id.iv_left, R.id.tv_right})
    public void onClickView(View view) {
        int id = view.getId();
        if (id == R.id.iv_left) {
            finish();
        } else if (id == R.id.tv_right) {
            onClearAllClicked((View) null);
        }
    }
}
